package com.projectexercise.book.service;

import com.projectexercise.book.enums.BookStatus;
import com.projectexercise.book.mapper.BookMapper;
import com.projectexercise.book.model.BookInfo;
import com.projectexercise.book.model.PageRequest;
import com.projectexercise.book.model.PageResponse;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
@Service
// 业务逻辑层
public class BookService {
    @Autowired
    private BookMapper bookMapper;
    public Integer insertBook(BookInfo bookInfo){
        return bookMapper.insertBook(bookInfo);
    }

    public PageResponse<BookInfo> getBookListByPage(PageRequest pageRequest) {
        Integer count = bookMapper.count();
        List<BookInfo> bookInfos = bookMapper.queryBookByPage(
                pageRequest.getOffset(), pageRequest.getPageSize());
        if(bookInfos.isEmpty()){
            return new PageResponse<>(null);
        }
        for (BookInfo book : bookInfos) {
            book.setStatusCN(BookStatus.getDescByCode(book.getStatus()).getDesc());
        }
        return new PageResponse<BookInfo>(bookInfos, count, pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        BookInfo book =  bookMapper.queryBookById(bookId);
        book.setStatusCN(BookStatus.getDescByCode(book.getStatus()).getDesc());
        return book;
    }

    public Integer updateBookInfo(BookInfo bookInfo) {
        return bookMapper.updateBookInfo(bookInfo);
    }

    public Integer batchDeleteBook(@RequestParam @Param("ids") List<Integer> ids) {
        return bookMapper.batchDeleteBook(ids);
    }
}
